﻿Imports System.Data
Imports System.Data.SqlClient
Public Class TelaDAO
    Implements ITela

    Private connection As SqlConnection
    Public Sub New(ByVal connection As SqlConnection)
        Me.connection = connection
    End Sub

    'Busca tela pelo ID do sistema
    Public Function BuscarTelaPorSistema(ByVal parCodigoTela As Integer) As List(Of Tela) Implements ITela.BuscarTelaPorSistema
        Dim listaTelas As New List(Of Tela)
        Me.connection.Open()
        Dim tela As New Tela
        Try
            Dim conn As New SqlCommand("select * from SISTEMATELA sit inner join TELAS tel on sit.SIT_TELA = tel.TEL_CODIGO where sit.SIT_SISTEMAS = @tel_sistema", Me.connection)
            conn.Parameters.AddWithValue("@tel_sistema", parCodigoTela)
            Dim dr As SqlDataReader = conn.ExecuteReader()
            While dr.Read()
                tela.ID_TELA = Convert.ToInt16(dr("TEL_CODIGO"))
                tela.NOME_TELA = dr("TEL_NOME").ToString()
                tela.INATIVA = Not Convert.ToInt16(dr("TEL_INATIVA")) = 0
                tela.DOCUMENTACAO = dr("TEL_DOCUMENTACAO").ToString()
                listaTelas.Add(tela)
            End While
            Me.connection.Close()
        Catch ex As Exception
        End Try

        Return listaTelas
    End Function
    'Lista todas as telas
    Public Function ListarTelas() As System.Collections.Generic.List(Of Tela) Implements ITela.ListarTelas
        Dim listaTelas As New List(Of Tela)
        Me.connection.Open()
        Try
            'SELECT TEL_CODIGO,TEL_NOME,TEL_INATIVA,TEL_D_CADASTRO,TEL_U_CADASTRO,TEL_D_ALTERACAO,TEL_U_ALTERACAO,TEL_DOCUMENTACAO FROM TELAS WHERE TEL_CODIGO = 238
            Dim conn As New SqlCommand("SELECT * FROM TELAS", Me.connection)
            Dim dr As SqlDataReader = conn.ExecuteReader()
            While dr.Read()
                Dim tela As New Tela
                tela.ID_TELA = Convert.ToInt16(dr("TEL_CODIGO"))
                tela.NOME_TELA = dr("TEL_NOME").ToString()
                tela.INATIVA = Not Convert.ToInt16(dr("TEL_INATIVA")) = 0
                tela.DOCUMENTACAO = dr("TEL_DOCUMENTACAO").ToString()
                listaTelas.Add(tela)
            End While
        Catch ex As Exception
        End Try
        Me.connection.Close()
        Return listaTelas
    End Function

End Class
